home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!genesis.demon.co.uk
- From: Lawrence Kirby <fred@genesis.demon.co.uk>
- Newsgroups: comp.lang.c
- Subject: Re: Input filename for fopen()?
- Date: Wed, 20 Mar 96 17:43:44 GMT
- Organization: none
- Message-ID: <827343824snz@genesis.demon.co.uk>
- References: <4iikju$nk8@lantana.singnet.com.sg>
- Reply-To: fred@genesis.demon.co.uk
- X-NNTP-Posting-Host: genesis.demon.co.uk
- X-Newsreader: Demon Internet Simple News v1.27
- X-Mail2News-Path: genesis.demon.co.uk
-
- In article <4iikju$nk8@lantana.singnet.com.sg>
- s8700055@singnet.com.sg "XY Xie" writes:
-
- >How do I get the user to input filename for fopen() ?
- >
- > I tried a gets(filename) before a fopen(filename,"r"). It worked for
- >files in the current dir (when the user only has to enter the
- >filename) , but it does not work for files in other dirs (when the
- >user has to enter something like "c:\\windows\\letter.txt").
-
- Did you try having the user enter:
-
- c:\windows\letter.txt
-
- or
-
- c:/windows/letter.txt ?
-
- >When I tried to type the filename directly into fopen(), it worked
- >fine for any file any where.
- >So I guess the problem lies in the ' \ ' because C treats it as an
- >escape sequence. How do I get around this?
-
- \ is an 'escape' characters in string literals in C source but that doesn't
- imply it has any special significance on file input/output. So unless your
- OS treats it specially you enter \ for \ and not \\ at runtime.
-
- Also gets() is dangerous and should never be used because you can't prevent
- it writing beyond the buffer you give it. Use fgets() instead but be sure
- you remove any trailing '\n' character before passing the string to fopen().
-
- --
- -----------------------------------------
- Lawrence Kirby | fred@genesis.demon.co.uk
- Wilts, England | 70734.126@compuserve.com
- -----------------------------------------
-